﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Utils.Menu;

namespace DexERP
{
    public partial class XtraFormLogin : DevExpress.XtraEditors.XtraForm
    {
        private string epicorCfgPath = @"C:\Epicor\ERP10.1Client\Client\config\Live.sysconfig";
        public XtraFormLogin()
        {
            InitializeComponent();
            
        }

        private void ButtonLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textEditPsw.Text) || string.IsNullOrEmpty(textEditUser.Text))
            {
                XtraMessageBox.Show("请输入用户名和密码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                //Ice.Core.Session _session = Login(epicorCfgPath, textEditUser.Text, textEditPsw.Text, "001");
                //if (_session != null)
                //{
                //    DexGlobal.User = _session;                    
                //    FormMain form = new FormMain();
                //    form.Show();
                //    this.Hide();
                //}
                FormMain form = new FormMain();
                form.Show();
                this.Hide();
            }
        }

        //public Ice.Core.Session Login(string confPath, string user, string password, string company = "")
        //{
        //    try
        //    {
        //        Ice.Core.Session _session = new Ice.Core.Session(user, password, Ice.Core.Session.LicenseType.Default, confPath);
        //        if (company != "")
        //        {
        //            _session.CompanyID = company;
        //        }
        //        return _session;
        //    }
        //    catch (Exception ex)
        //    {
        //        XtraMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //        return null;
        //    }
        //}

        private void XtraFormLogin_Load(object sender, EventArgs e)
        {
            //var Items = DexGlobal.SysCfg.Conns.Select(o => o.caption).ToArray();
            //this.comboBoxDb.Properties.Items.AddRange(Items);
            comboBoxDb.SelectedIndex = 0;
        }

        private void comboBoxDb_SelectedIndexChanged(object sender, EventArgs e)
        {
            var Item = DexGlobal.SysCfg.Conns.Where(o=>o.caption==comboBoxDb.Text).First();
            epicorCfgPath = Item.cfgpath;
            DexGlobal.Database = Item;
        }
    }
}